package tdca.util;

import java.io.IOException;
import android.util.Log;

public class DataUtil {
	private static DataUtil dataUtil;
	
	private String masterCode;
    private String privateCode;
    private String slaveCode;
    
	private String[] masterInfo;
	private String masterCodeHash;
	private String masterCodePrivateInfo;
	private String[] privateInfo;
	private String privateCodeHash;
	private String slaveCodeDecrypt;
	
	private DataUtil(){
		dataUtil = this;
	}
	
	public static DataUtil instance(){
		if(dataUtil == null)
			dataUtil = new DataUtil();
		return dataUtil;
	}
	
	public void clearAll(){
		dataUtil = null;
		dataUtil = new DataUtil();
	}
	
	public boolean isThreeCode(){
        if(dataUtil.getMasterCode()!=null&&dataUtil.getPrivateCode()!=null&&dataUtil.getSlaveCode()!=null){
            return true;
        }
        return false;
    }
	
	public void PSmasterCode(){
        String[] splitedMasterCode = null;
        splitedMasterCode = dataUtil.getMasterCode().split("/");

        dataUtil.setMasterInfo(splitedMasterCode);
        dataUtil.setMasterCodeHash(SHA1.toSHA1(dataUtil.getMasterCode()));
        dataUtil.setMasterCodePrivateInfo(splitedMasterCode[splitedMasterCode.length-1]);
    }
	
	public boolean PSprivateCode() throws IOException{
        String privateInfo = null;
        if(dataUtil.getPrivateCode().length() == 173){
            privateInfo = RSA.Dencrypt(BASE64.decodeStringToByteArr(dataUtil.getPrivateCode()));
            Log.v("privateInfo",privateInfo);
            dataUtil.setPrivateInfo(privateInfo.split("/"));
            dataUtil.setPrivateCodeHash(SHA1.toSHA1(dataUtil.getPrivateCode()));

            return true;
        }
        return false;
    }

    public boolean PSslaveCode() throws IOException{
        String decrypted = null;

        if(dataUtil.getSlaveCode().length() == 173){
            decrypted = RSA.Dencrypt(BASE64.decodeStringToByteArr(dataUtil.getSlaveCode()));
            Log.v("slaveDF", decrypted);
            dataUtil.setSlaveCodeDecrypt(decrypted);
            return true;
        }
        return false;
    }
    
    public boolean masterInfoHF_Match_slaveCodeDF(){
    	if(dataUtil.getMasterCodeHash() != null && dataUtil.getSlaveCodeDecrypt() != null && dataUtil.getMasterCodeHash().equals(dataUtil.getSlaveCodeDecrypt())){
    		return true;
    	}
    	return false;
    }
    
    public boolean privateCodeHF_Match_masterCodePI(){
    	if(dataUtil.getPrivateCodeHash() != null && dataUtil.getMasterCodePrivateInfo() != null && dataUtil.getPrivateCodeHash().equals(dataUtil.getMasterCodePrivateInfo())){
    		return true;
    	}
    	return true;
    }

	public String getMasterCode() {
		return masterCode;
	}

	public void setMasterCode(String masterCode) {
		this.masterCode = masterCode;
	}

	public String getPrivateCode() {
		return privateCode;
	}

	public void setPrivateCode(String privateCode) {
		this.privateCode = privateCode;
	}

	public String getSlaveCode() {
		return slaveCode;
	}

	public void setSlaveCode(String slaveCode) {
		this.slaveCode = slaveCode;
	}

	public String[] getMasterInfo() {
		return masterInfo;
	}

	public void setMasterInfo(String[] masterInfo) {
		this.masterInfo = masterInfo;
	}

	public String getMasterCodeHash() {
		return masterCodeHash;
	}

	public void setMasterCodeHash(String masterCodeHash) {
		this.masterCodeHash = masterCodeHash;
	}

	public String getMasterCodePrivateInfo() {
		return masterCodePrivateInfo;
	}

	public void setMasterCodePrivateInfo(String masterCodePrivateInfo) {
		this.masterCodePrivateInfo = masterCodePrivateInfo;
	}

	public String[] getPrivateInfo() {
		return privateInfo;
	}

	public void setPrivateInfo(String[] privateInfo) {
		this.privateInfo = privateInfo;
	}

	public String getPrivateCodeHash() {
		return privateCodeHash;
	}

	public void setPrivateCodeHash(String privateCodeHash) {
		this.privateCodeHash = privateCodeHash;
	}

	public String getSlaveCodeDecrypt() {
		return slaveCodeDecrypt;
	}

	public void setSlaveCodeDecrypt(String slaveCodeDecrypt) {
		this.slaveCodeDecrypt = slaveCodeDecrypt;
	}
	
}
